package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.commen.Result;
import com.itheima.entiry.ShoppingCart;
import com.itheima.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        Long user = (Long) session.getAttribute("user");
        shoppingCart.setUserId(user);
        int number=1;
        shoppingCart.setNumber(number);
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        ShoppingCart one = shoppingCartService.getOne(qw);
        if (one!=null&&one.getNumber()>0){
            int dishNumber=one.getNumber();
            dishNumber++;
            shoppingCart.setNumber(dishNumber);
            one.setNumber(dishNumber);
            shoppingCartService.update(one,qw);
        }else {
            shoppingCartService.save(shoppingCart);
        }
        return new Result(1,shoppingCart);
    }

    @GetMapping("/list")
    public Result getAll(HttpSession session){
        Long user = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,user);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(qw);
        return new Result(1,shoppingCartList);
    }

    @PostMapping("/sub")
    public Result sub(@RequestBody ShoppingCart shoppingCart){
//        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
//        qw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId())
//                .eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
//        ShoppingCart cart = shoppingCartService.getOne(qw);
//        if (cart.getNumber().equals(1)){
//            shoppingCartService.removeById(cart.getId());
//        }else {
//            int number=cart.getNumber();
//            number--;
//            cart.setNumber(number);
//        }
//        return new Result(1,cart);
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        Long dishId = shoppingCart.getDishId();
        if (dishId!=null){
            qw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,dishId);
        }else {
            qw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart cart = shoppingCartService.getOne(qw);
//        if (cart.getNumber().equals(1)){
//            shoppingCartService.removeById(cart.getId());
//        }else {
//            int number=cart.getNumber();
//            number--;
//            cart.setNumber(number);
//        }
        int number=cart.getNumber();
        number--;
        cart.setNumber(number);
        if (number>0){
            shoppingCartService.update(cart,qw);
        }
        else {
            shoppingCartService.removeById(cart.getId());
        }
        return new Result(1,cart);
    }

    @DeleteMapping("/clean")
    public Result cleanShop(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(qw);
        return new Result(1);
    }
}
